
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.events import EVENT_JOB_EXECUTED, EVENT_JOB_ERROR
from log_utils import AsyncLogger

logger = AsyncLogger()
def setup_scheduler():
    """配置并返回配置好的AsyncIOScheduler实例"""
    scheduler = AsyncIOScheduler()

    # 添加事件监听器
    def event_listener(event):
        if event.code == EVENT_JOB_EXECUTED:
            logger.info(f"任务 {event.job_id} 执行成功")
        elif event.code == EVENT_JOB_ERROR:
            logger.error(f"任务 {event.job_id} 执行失败: {event.exception}")

    scheduler.add_listener(event_listener,
                           EVENT_JOB_EXECUTED | EVENT_JOB_ERROR)
    return scheduler